package com.probox.common.entity.service.platfom.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.PlatformException;
import com.probox.common.core.utils.DateUtils;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.platfom.PlatformTemplate;
import com.probox.common.entity.domain.platfom.vo.api.PlatformTemplateView;
import com.probox.common.entity.service.platfom.IPlatformTemplateService;
import com.probox.common.entity.service.platfom.IPlatformInfoTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.platfom.PlatformInfoTemplateMapper;
import com.probox.common.entity.domain.platfom.PlatformInfoTemplate;
import org.springframework.transaction.annotation.Transactional;

/**
 * 平台模板中间Service业务层处理
 *
 *
 * @date 2021-06-16
 */
@Service
@Transactional
public class PlatformInfoTemplateServiceImpl extends ServiceImpl<PlatformInfoTemplateMapper, PlatformInfoTemplate> implements IPlatformInfoTemplateService {
    @Autowired
    private PlatformInfoTemplateMapper platformInfoTemplateMapper;

    @Autowired
    private IPlatformTemplateService iPlatformTemplateService;

    /**
     * 查询平台模板中间
     *
     * @param platformTemplateUid 平台模板中间ID
     * @return 平台模板中间
     */
    @Override
    public PlatformInfoTemplate selectPlatformInfoTemplateById(Long platformTemplateUid) {
        return this.getById(platformTemplateUid);
    }



    /**
     * 查询平台模板中间列表
     *
     * @param platformInfoTemplate 平台模板中间
     * @return 平台模板中间
     */
    @Override
    public List<PlatformInfoTemplate> queryList(PlatformInfoTemplate platformInfoTemplate) {
        QueryWrapper<PlatformInfoTemplate> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(platformInfoTemplate.getTemplateUid())) && platformInfoTemplate.getTemplateUid() != null) {
            queryWrapper.eq("TEMPLATE_UID", platformInfoTemplate.getTemplateUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformInfoTemplate.getPlatformUid())) && platformInfoTemplate.getPlatformUid() != null) {
            queryWrapper.eq("PLATFORM_UID", platformInfoTemplate.getPlatformUid());
        }
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        List<PlatformInfoTemplate> platformInfoTemplates = platformInfoTemplateMapper.selectList(queryWrapper);
        return platformInfoTemplates;
    }


    @Override
    public List<PlatformInfoTemplate> queryListSs(PlatformInfoTemplate platformInfoTemplate) {
        List<PlatformInfoTemplate> platformInfoTemplates = platformInfoTemplateMapper.selectListF(platformInfoTemplate);
        return platformInfoTemplates;
    }

    @Override
    public List<PlatformInfoTemplate> queryListS(PlatformInfoTemplate platformInfoTemplate) {
        QueryWrapper<PlatformInfoTemplate> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(platformInfoTemplate.getTemplateUid())) && platformInfoTemplate.getTemplateUid() != null) {
            queryWrapper.eq("TEMPLATE_UID", platformInfoTemplate.getTemplateUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformInfoTemplate.getPlatformUid())) && platformInfoTemplate.getPlatformUid() != null) {
            queryWrapper.eq("PLATFORM_UID", platformInfoTemplate.getPlatformUid());
        }
        //queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        return platformInfoTemplateMapper.selectList(queryWrapper);
    }

    /**
     * 查询最新的草稿模板
     * @param platformInfoTemplate
     * @return
     */
    @Override
    public PlatformInfoTemplate checkTemplate(PlatformInfoTemplate platformInfoTemplate){
        QueryWrapper<PlatformInfoTemplate> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(platformInfoTemplate.getTemplateUid())) && platformInfoTemplate.getTemplateUid() != null) {
            queryWrapper.eq("TEMPLATE_UID", platformInfoTemplate.getTemplateUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformInfoTemplate.getPlatformUid())) && platformInfoTemplate.getPlatformUid() != null) {
            queryWrapper.eq("PLATFORM_UID", platformInfoTemplate.getPlatformUid());
        }
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        PlatformInfoTemplate template = platformInfoTemplateMapper.selectOne(queryWrapper);
        if(ObjectUtil.isEmpty(template)){// 如果为空,有可能是模板被覆盖,去查询该模板的来源的最新草稿模板id
            PlatformTemplate byId = iPlatformTemplateService.getById(platformInfoTemplate.getTemplateUid());
            if(ObjectUtil.isEmpty(byId)) return template;
            HashMap map = new HashMap();
            map.put("platformUid",platformInfoTemplate.getPlatformUid());
            map.put("sourceTemplateId",byId.getSourceTemplateId());
            return platformInfoTemplateMapper.queryDraft(map);
        }
        return template;
    }


    /**
     * 新增平台模板中间
     *
     * @param platformInfoTemplate 平台模板中间
     * @return 结果
     */
    @Override
    public boolean insertPlatformInfoTemplate(PlatformInfoTemplate platformInfoTemplate, PlatformTemplateView view) {
        deletePlatformInfoTemplateByIds(platformInfoTemplate.getPlatformUid(),platformInfoTemplate.getTemplateUid());
        platformInfoTemplate.setDataStatus(DataStatus.valid.getKey());
        platformInfoTemplate.setCreateUid(view.getUserId());
        platformInfoTemplate.setCreateName(view.getUserName());
        platformInfoTemplate.setCreateTime(view.getTime());
        platformInfoTemplate.setUpdateUid(view.getUserId());
        platformInfoTemplate.setUpdateName(view.getUserName());
        platformInfoTemplate.setUpdateTime(view.getTime());
        return this.save(platformInfoTemplate);

    }

    /**
     * 修改平台模板中间
     *
     * @param platformInfoTemplate 平台模板中间
     * @return 结果
     */
    @Override
    public boolean updatePlatformInfoTemplate(PlatformInfoTemplate platformInfoTemplate) {

        return this.updateById(platformInfoTemplate);
    }

    /**
     *  删除中间表数据
     * @param platformUid
     * @param templateUid
     */
    @Override
    public void deletePlatformInfoTemplateByIds(Long platformUid,Long templateUid) {
        UpdateWrapper<PlatformInfoTemplate> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("PLATFORM_UID", platformUid);
        updateWrapper.eq("TEMPLATE_UID", templateUid);
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        this.update(updateWrapper);
    }

    @Override
    public void removePlatformInfoTemplateByIds(Long platformUid,List<Long> aLongs,Long templateUid) {
        UpdateWrapper<PlatformInfoTemplate> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("PLATFORM_UID", platformUid);
        updateWrapper.in("TEMPLATE_UID",aLongs);
        updateWrapper.ne("TEMPLATE_UID", templateUid);
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        this.update(updateWrapper);
    }


}
